//导入模型
const { Class_Target } = require('../../models/Course outline/class_Target')
const { T_exam_target } = require('../../models/t_exam_target')

//查找
exports.Get_Class_Target = async (req, res) => {
  const { t_user_and_a_c_s_id } = req.query;

  try {
    const classes = await T_exam_target.findAll({
      where: {
        t_user_and_a_c_s_id: t_user_and_a_c_s_id,
      },
    });

    if (classes.length === 0) {
      return res.json({
        'code': 203,
        'msg': '没有对应数据'
      });
    }

    res.json({
      'code': 200,
      'msg': '查询成功',
      'data': classes
    });
  } catch (error) {
    console.error('查询数据失败:', error);
    res.json({
      'code': 500,
      'msg': '查询失败'
    });
  }
};

// 添加
exports.ADD_Class_Introduce = async (req, res) => {
  const { target_name, target_content, t_user_and_a_c_s_id } = req.body;

  try {
    // 检查是否存在关于t_user_and_a_c_s_id的数据
    const existingData = await Class_Target.findOne({ where: { target_name, t_user_and_a_c_s_id } });

    if (existingData) {
      // 更新数据
      await Class_Target.update({ target_content }, { where: { target_name, t_user_and_a_c_s_id } });
      res.json({
        code: 200,
        msg: '数据已c插入'
      });
    } else {
      // 插入新数据
      await Class_Target.create({ target_name, target_content, t_user_and_a_c_s_id });
      res.json({
        code: 200,
        msg: '数据已插入'
      });
    }
  } catch (error) {
    console.error('操作失败:', error);
    res.json({
      code: 500,
      msg: '操作失败'
    });
  }
};